/**
 * Created by dukes on 2015/12/11.
 */
class SetupPanel extends eui.Component{
    public constructor(){
        super();
        this.skinName = "pet.SetupPanelSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAdd, this );
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemove, this);
    }

    public updateDisplay(evt:egret.Event=null){
        var monitor:PerformanceMonitor = PerformanceMonitor.getInstance();
        this.img_msc.source = monitor.getSwithState(PerformanceMonitor.MUSIC) ? "msc_close" : "msc_open";
        this.img_snd.source = monitor.getSwithState(PerformanceMonitor.SOUND) ? "snd_close" : "snd_open";
        this.img_efct.source = monitor.getSwithState(PerformanceMonitor.EFFECTS) ? "effect_close" : "effect_open";
        this.img_msc_s.source = monitor.getSwithState(PerformanceMonitor.MUSIC) ? "label_close" : "label_open";
        this.img_snd_s.source = monitor.getSwithState(PerformanceMonitor.SOUND) ? "label_close" : "label_open";
        this.img_efct_s.source = monitor.getSwithState(PerformanceMonitor.EFFECTS) ? "label_close" : "label_open";
    }

    private onMusic(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        var monitor:PerformanceMonitor = PerformanceMonitor.getInstance();
        var targetValue = !monitor.getSwithState(PerformanceMonitor.MUSIC);
        this.img_msc.source = targetValue ? "msc_close" : "msc_open";
        this.img_msc_s.source = targetValue ? "label_close" : "label_open";
        monitor.setSwithState(PerformanceMonitor.MUSIC,targetValue);
        if( !targetValue){
            SoundMgr.getInstance().playBg();
        }
        if( targetValue != Model.getStateFromMask(GameConst.MARK_MUSIC)){
            if( !targetValue ){
                window["__clearMark"](GameConst.MARK_MUSIC);
            }else{
                window["__setMark"](GameConst.MARK_MUSIC);
            }
        }
        if( !Model.getStateFromMask(GameConst.MARK_FORIDE_LOWMODEL)){
            window["__setMark"](GameConst.MARK_FORIDE_LOWMODEL);
        }
    }
    private onSound(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        var monitor:PerformanceMonitor = PerformanceMonitor.getInstance();
        var targetValue = !monitor.getSwithState(PerformanceMonitor.SOUND);
        this.img_snd.source = targetValue ? "snd_close" : "snd_open";
        this.img_snd_s.source = targetValue ? "label_close" : "label_open";
        monitor.setSwithState(PerformanceMonitor.SOUND,targetValue)
        if( targetValue != Model.getStateFromMask(GameConst.MARK_SOUND)){
            if( !targetValue ){
                window["__clearMark"](GameConst.MARK_SOUND);
            }else{
                window["__setMark"](GameConst.MARK_SOUND);
            }
        }
        if( !Model.getStateFromMask(GameConst.MARK_FORIDE_LOWMODEL)){
            window["__setMark"](GameConst.MARK_FORIDE_LOWMODEL);
        }
    }
    private onEffect(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        var monitor:PerformanceMonitor = PerformanceMonitor.getInstance();
        var targetValue = !monitor.getSwithState(PerformanceMonitor.EFFECTS);
        this.img_efct.source = targetValue ? "effect_close" : "effect_open";
        this.img_efct_s.source = targetValue ? "label_close" : "label_open";
        monitor.setSwithState(PerformanceMonitor.EFFECTS,targetValue)
        if( targetValue != Model.getStateFromMask(GameConst.MARK_EFFECT)){
            if( !targetValue ){
                window["__clearMark"](GameConst.MARK_EFFECT);
            }else{
                window["__setMark"](GameConst.MARK_EFFECT);
            }
        }
        if( !Model.getStateFromMask(GameConst.MARK_FORIDE_LOWMODEL)){
            window["__setMark"](GameConst.MARK_FORIDE_LOWMODEL);
        }
    }

    public onJoin(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        window["__joinGroup"]();
    }

    public onLogout(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        window["logout"]();
    }

    private onClose(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        DisplayController.instance().removeFromAlert(GameConst.GrayMask);
        DisplayController.instance().removeFromAlert(this);
    }

    public show(){
        DisplayController.instance().addToAlert(GameConst.GrayMask);
        DisplayController.instance().addToAlert(this);
    }

    private img_close:eui.Image;
    private img_head:eui.Image;
    private img_msc:eui.Image;
    private img_snd:eui.Image;
    private img_efct:eui.Image;
    private img_msc_s:eui.Image;
    private img_snd_s:eui.Image;
    private img_efct_s:eui.Image;
    private btn_join:eui.Image;
    private lab_nickname:eui.Label;
    private lab_id:eui.Label;
    private btn_logout:eui.Label;
    public partAdded(name:string, instance:any) {
        super.partAdded(name, instance);
        if (name == "img_close") {
            this.img_close = instance;
        } else if (name == "img_head") {
            this.img_head = instance;
            if( Model.me.platform != PlatformEnum.QQBROWSER){
                this.img_head.source = Model.me.figureurl;
            }else{
                this.img_head.source = RES.getRes("default_head_"+(Model.me.id%5));
            }
        }else  if( name == "img_msc"){
            this.img_msc = instance;
        }else  if( name == "btn_logout"){
            this.btn_logout = instance;
            this.btn_logout.visible = Model.me.platform == PlatformEnum.QQBROWSER;
        }else if( name == "img_snd"){
            this.img_snd = instance;
        }else  if( name == "lab_id"){
            this.lab_id = instance;
            this.lab_id.text = LimitStringLen(Model.me.id+"");
        }else if( name == "lab_nickname"){
            this.lab_nickname = instance;
            this.lab_nickname.text = LimitStringLen(Model.me.name);
        }else  if( name == "img_efct_s"){
            this.img_efct_s = instance;
        }else if( name == "img_snd_s"){
            this.img_snd_s = instance;
        }else  if( name == "img_msc_s"){
            this.img_msc_s = instance;
        }else if( name == "img_efct"){
            this.img_efct = instance;
        }else if( name == "btn_join"){
            this.btn_join = instance;
            this.btn_join.visible = Model.me.platform != PlatformEnum.QQBROWSER;
        }
    }

    private onAdd(evt:egret.Event){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_msc.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onMusic, this);
        this.img_snd.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSound, this);
        this.img_efct.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onEffect, this);
        this.btn_join.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onJoin, this);
        this.btn_logout.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onLogout, this);
        EventController.getInstance().addEventListener(EventController.LOW_FRAMERATE, this.updateDisplay, this);
        this.updateDisplay();
    }

    private onRemove(evt:egret.Event){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.img_close.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_msc.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onMusic, this);
        this.img_snd.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onSound, this);
        this.img_efct.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onEffect, this);
        this.btn_logout.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onLogout, this);
        EventController.getInstance().removeEventListener(EventController.LOW_FRAMERATE, this.updateDisplay, this);
        this.btn_join.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onJoin, this);
    }

    public measure() {
        super.measure();
        this.x = GameConst.StageW - this.measuredWidth >> 1;
        this.y = GameConst.StageH - this.measuredHeight >> 1;
    }
    private static _instance:SetupPanel;
    public static getInstance():SetupPanel{
        if( SetupPanel._instance == null )
            SetupPanel._instance = new SetupPanel();
        return SetupPanel._instance;
    }
}